<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use Hyperf\Scout\Searchable;
use Mine\MineModel;

/**
 * @property int $id 聊天记录ID
 * @property int $main_uid 主账号ID
 * @property int $user_id 客服ID（0:代表待分配客服消息 >0: 用户ID）
 * @property int $talk_type 聊天类型：1=私聊 2=群聊
 * @property string $msg_type 消息类型[1:文本消息;2:文件消息;3:会话消息;4:代码消息;5:投票消息;6:群公告;7:好友申请;8:登录通知;9:入群消息/退群消息;]
 * @property array $msg_data 消息原始数据
 * @property int $msg_id 企微发送的消息ID
 * @property int $robot_id 机器人ID
 * @property string $from_room_id 群ID
 * @property string $sender 发送者ID
 * @property string $receiver 接收者ID
 * @property int $is_revoke 是否撤回消息[0:否;1:是]
 * @property int $is_cs 是否客服发送：[0:否;1:是;]
 * @property int $is_need_verify 是否需要认证
 * @property string $need_verify_msg 认证文案
 * @property int $quote_id 引用消息ID
 * @property array $content 消息内容
 * @property string $text 文字内容
 * @property array $warn_users @好友
 * @property string $msg_unique_identifier 消息ID撤回消息时需要用到
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property-read null|ChRobot $robot 
 * @property-read null|ChCustomerService $customerService 
 */
class ChTalkRecord extends MineModel
{
//    use Searchable;

    /**
     * 客服发送key
     */
    const CUSTOMER_SERVICE_SENT = 'customer_service_sent';

    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_talk_records';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'main_uid', 'user_id', 'talk_type', 'msg_type', 'msg_data', 'msg_id', 'robot_id', 'from_room_id', 'sender', 'receiver', 'is_revoke', 'is_cs', 'is_need_verify', 'need_verify_msg', 'quote_id', 'content', 'text', 'warn_users', 'msg_unique_identifier', 'created_at', 'updated_at'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'main_uid' => 'integer', 'user_id' => 'integer', 'talk_type' => 'integer', 'msg_data' => 'array', 'msg_id' => 'integer', 'robot_id' => 'integer', 'is_revoke' => 'integer', 'is_cs' => 'integer', 'is_need_verify' => 'integer', 'quote_id' => 'integer', 'content' => 'array', 'warn_users' => 'array', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    public function dynamicRelation()
    {
        return match($this->type) {
            'type_a' => $this->hasOne(ChRobot::class, 'id','user_id'),
            'type_b' => $this->hasOne(ChContact::class, 'id','user_id'),
            default => null,
        };
    }

    /**
     * 定义索引中的可搜索字段
     * @return string[]
     */
    public function toSearchableArray(): array
    {
        return [
            'id' => $this->id,
            'robot_id' => $this->robot_id,
            'talk_type' => $this->talk_type,
            'receiver' => $this->receiver,
            'sender' => $this->sender,
            'from_room_id' => $this->from_room_id,
            'msg_type' => $this->msg_type,
            'text' => $this->text, // 这是您要搜索的主要字段
            'content' => $this->content,
        ];
    }

//    public function contact()
//    {
//        return $this->hasOne(ChContact::class, 'robot_id', 'robot_id')
//        ->where(function ($query) {
//            $query->whereColumn('ch_contacts.user_id', 'ch_talk_records.sender')
//                ->orWhereColumn('ch_contacts.user_id', 'ch_talk_records.receiver');
//        });
//    }

    public function customerService(): \Hyperf\Database\Model\Relations\HasOne
    {
        return $this->hasOne(ChCustomerService::class, 'id', 'user_id');
    }

    public function robot(): \Hyperf\Database\Model\Relations\HasOne
    {
        return $this->hasOne(ChRobot::class, 'id', 'robot_id');
    }
}
